package com.edu.kindergarten.adapter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;

import com.edu.kindergarten.R;
import com.edu.kindergarten.config.Constants;
import com.edu.kindergarten.utils.LogUtils;
import com.edu.kindergarten.utils.ScreenUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SingleImageAdapter extends BaseAdapter {

	private static final String TAG = "SingleImageAdapter";

	private Context context;
	private int layoutId;
	private List<String> listData;
	private int to[];
	private boolean multiSelect = false;
	
	public SparseBooleanArray checkedIdxList;
	private int imageWidth;
	
	private CheckedChangeListener checkedChangeListener;
	private int mCountAvail = Constants.SELECT_IMAGE_MAX_NUMBER;
	
	public SingleImageAdapter(Context context, int layoutId,
			List<String> listData, int to[]) {
		this.context = context;
		this.layoutId = layoutId;
		this.listData = listData;
		this.to = to;
		
		setDefaultWidth();
		
		for (int i = 0; i < listData.size(); i++) {
			LogUtils.d(TAG, "listData,path:" + listData.get(i));
		}
	}
	
	public SingleImageAdapter(Context context, int layoutId,
			List<String> listData, int to[],int countAvail) {
		this.context = context;
		this.layoutId = layoutId;
		this.listData = listData;
		this.to = to;
		this.multiSelect = true;
		mCountAvail = countAvail;
		checkedIdxList = new SparseBooleanArray();
		
		setDefaultWidth();
	}
	
	/*public void setCountAvail(int countAvail){
		mCountAvail = countAvail;
	}*/
	
	private void setDefaultWidth(){
		//we have 3 columns, and each column has left/right border
		imageWidth = ScreenUtils.getScreenWidth(context)/3 - 4*2;
	}
	
	public void setImageWidth(int width){
		imageWidth = width;
	}
	
	public void setCheckChangeListener(CheckedChangeListener listener){
		checkedChangeListener = listener;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		
		LogUtils.d(TAG, "getView,position:" + position);

		if (convertView == null) {
			LayoutInflater layoutInflator = LayoutInflater.from(context);
			convertView = layoutInflator.inflate(layoutId,parent,false);
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView.findViewById(to[0]);
			
			if (multiSelect) {
				LayoutParams params = new LayoutParams(imageWidth, imageWidth);
				holder.imageView.setLayoutParams(params);
				holder.checkBox = (CheckBox) convertView.findViewById(to[1]);
				
				final int pos = position;
				holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(CompoundButton arg0, boolean checked) {
						// TODO Auto-generated method stub
						LogUtils.d(TAG, "getView checkedIdxList.size():" + checkedIdxList.size() +
								",position:" + pos +
								",checked:" + checked);
						if (checked) {
							if (checkedIdxList.size() < mCountAvail) {
								if (checkedIdxList.get(pos,false) == false) {
									checkedIdxList.append(pos, true);
								}
							}else {
								//uncheck checkbox
								holder.checkBox.toggle();
							}
						}else {
							if (checkedIdxList.get(pos,false)) {
								checkedIdxList.delete(pos);
							}
						}
						if (checkedChangeListener != null) {
							checkedChangeListener.OnCheckBoxChangeListener(pos,checked,checkedIdxList);
						}
					}
				});
			}
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		//holder.imageView.setImageResource(listData.get(position).image);
		//String path = listData.get(position);
		LogUtils.d(TAG, "getView,path:" + listData.get(position));
		ImageLoader.getInstance().displayImage(listData.get(position), holder.imageView);
		

		return convertView;
	}

	public class ViewHolder {
		public ImageView imageView;
		public CheckBox checkBox;
	}
	
	public interface CheckedChangeListener {
		public void OnCheckBoxChangeListener(int position,boolean checked,SparseBooleanArray checkedIdxList);
	}

}
